﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ershou.BLL;
using QRCoder;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public partial class yhdd : System.Web.UI.Page
{
    sp sp1 = new sp();
    yh yh1 = new yh();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Key"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        if (!IsPostBack)
        {
            Bind();  //调用自定义方法Bind()
        }
    }

    protected void Bind()
    {
        var orders = yh1.GetAllyhdd(int.Parse(Session["CustomerId"].ToString().Trim()));
        if (orders.Count() == 0)
        {
            Panel1.Visible = false;
            lblOrder.Text = "尚无报名的拍卖！";
        }
        else
        {
            Panel1.Visible = true;
            lblOrder.Text = "";
        }
        GridView1.DataSource = orders;
        GridView1.DataBind();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Bind();  //调用自定义方法Bind()
    }

    protected void button3_Click(object sender, EventArgs e)
    {
        int sum = 0;
        GridView gvOrder = new GridView();
        gvOrder = (GridView)Page.Master.FindControl("ContentPlaceHolder1").FindControl("GridView1");
        if (gvOrder != null)
        {
            for (int i = 0; i < gvOrder.Rows.Count; i++)
            {
                CheckBox chkChoice = new CheckBox();
                chkChoice = (CheckBox)gvOrder.Rows[i].FindControl("CheckBox1");
                if (chkChoice != null)
                {
                    if (chkChoice.Checked)
                    {
                        sum=sum - int.Parse(gvOrder.Rows[i].Cells[2].Text) + int.Parse(gvOrder.Rows[i].Cells[4].Text);
                        int OrderId = int.Parse(gvOrder.Rows[i].Cells[0].Text);
                        sp1.UpdateOrder5(OrderId, int.Parse(Session["CustomerId"].ToString().Trim()));
                    }
                }
            }
            Image1.Visible = true;
            if (sum > 0)
            {
                string content = "一共缴纳" + sum.ToString() + "元";// 替换为您想要生成二维码的内容
                Bitmap qrCodeBitmap = GenerateQRCode(content, 200, 200); // 生成二维码Bitmap
                Image1.ImageUrl = ConvertBitmapToImageUrl(qrCodeBitmap);
            }
            else
            {
                string content = "一共归还您" + ((-1) * sum).ToString() + "元"; // 替换为您想要生成二维码的内容
                Bitmap qrCodeBitmap = GenerateQRCode(content, 200, 200); // 生成二维码Bitmap
                Image1.ImageUrl = ConvertBitmapToImageUrl(qrCodeBitmap);
            }
        }
        Bind();
    }
    private Bitmap GenerateQRCode(string content, int width, int height)
    {
        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); // 正确引用ECCLevel
        QRCode qrCode = new QRCode(qrCodeData);

        // 假设您想要使用黑色和白色
        Color darkColor = Color.White; // 暗色部分使用黑色
        Color lightColor = Color.Black; // 亮色部分使用白色

        return qrCode.GetGraphic(width, lightColor, darkColor, true);
    }

    private string ConvertBitmapToImageUrl(Bitmap bitmap)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            bitmap.Save(ms, ImageFormat.Png); // 将Bitmap保存到MemoryStream中
            byte[] imageBytes = ms.ToArray(); // 将MemoryStream转换为字节数组

            // 将字节数组转换为Base64字符串
            string base64String = Convert.ToBase64String(imageBytes);
            return "data:image/png;base64," + base64String; // 返回Base64编码的图像URL
        }
    }
}